using Godot;
using System;

public partial class HitFlashComponent : Node
{
	[Export]
	HealthComponent healthComponent;
	[Export]
	Sprite2D sprite;
	[Export]
	ShaderMaterial hitFlashMaterial;

	Tween hitFlashTween;


	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		healthComponent.HealthChanged += OnHealthChanged;
		sprite.Material = hitFlashMaterial;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public void OnHealthChanged(){
		if( hitFlashTween != null && hitFlashTween.IsValid()){
			hitFlashTween.Kill();
		}
		(sprite.Material as ShaderMaterial).SetShaderParameter("lerp_percent",1.0);
		hitFlashTween = CreateTween();
		hitFlashTween.TweenProperty(sprite.Material,"shader_parameter/lerp_percent",0.0,.25)
		.SetEase(Tween.EaseType.In).SetTrans(Tween.TransitionType.Cubic);
	}
}
